// pages/myself/balance/balance.js
var network = require('../../../utils/network_util.js');
var api = require('../../../utils/api.js');
var utils = require('../../../utils/utils.js');
var Zan = require('../../../dist/index');
var start = 0;
const app = getApp();

Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    account: [],
    radioCheckVal: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
         * 获取个人及商家信息
         */
    if (app.globalData.userAndTenantInfo) {
      this.setData({
        balance: app.globalData.userAndTenantInfo.balance
      })
    } else {
      app.userAndTenantInfoCallback = res => {
        this.setData({
          balance: res.balance
        })
      }
    }

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: "余额",
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      account: [],
    });
    network._post_page_json(this, api.getBills, start, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.account.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.getBills, start, {}, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  balanceDetails: function (e) {
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    if (type == 0) {
      wx.navigateTo({
        url: '/pages/myself/balance/refundDetails?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/myself/balance/balanceDetails?id=' + id
      })
    }
  },

  openMember: function () {
    wx.navigateTo({
      url: '/pages/myself/balance/openMember',
    })
  }
}))

// 获取明细列表结果
var success = function (that, res) {
  for (var i = 0; i < res.data.data.length; i++) {
    res.data.data[i].creatTime = utils.formatDate(new Date(res.data.data[i].createDate))
  }
  if (res.data.data) {
    that.setData({
      account: that.data.account.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}
